using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.MF.DataAccess;
using WP.MF.DataEntity;
namespace WP.MF.BusinessRule
{
    public class MOutLookAddinMenuHdrBRL:RuleBase
    {
        public MOutLookAddinMenuHdrBRL()
		{
            //
            // TODO: Add constructor logic here
            //
        }
        #region CommonValid
        public bool CommonValid(DBAccess dBAccess, MOutLookAddinMenuHdrDEL MOutLookAddinMenuHdrDEL)
        {
            bool bReturn = true;

            return bReturn;
        }
        #endregion CommonValid
        #region Insert MOutLookAddinMenuHdr Valid
        public bool InsertValid(DBAccess dBAccess, MOutLookAddinMenuHdrDEL MOutLookAddinMenuHdrDEL)
        {
            bool bReturn = true;
            string strErrPrompt = string.Empty;
            SysVar.ErrorMessage.Clear();

            DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MOutLookAddinMenuHdrDEL.TABLENAME + "'").DefaultView;
            foreach (DataRowView drw in dvwCheck)
            {
                if (!FieldCheck(dBAccess, MOutLookAddinMenuHdrDEL.Tables[MOutLookAddinMenuHdrDEL.TABLENAME ].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.AddNew))
                {
                    bReturn = false;
                    if (SysVar.ErrorMessage.Count == 0)
                    {
                        SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                    }
                    break;
                }
            }
            bReturn &= CommonValid(dBAccess, MOutLookAddinMenuHdrDEL);

            return bReturn;
        }
        #endregion
        #region Update MOutLookAddinMenuHdr Valid
        public bool UpdateValid(DBAccess dBAccess, MOutLookAddinMenuHdrDEL MOutLookAddinMenuHdrDEL)
        {
            bool bReturn = true;
            string strErrPrompt = string.Empty;
            SysVar.ErrorMessage.Clear();

            if (IsExistedAndNotChanged(dBAccess, MOutLookAddinMenuHdrDEL.Tables[MOutLookAddinMenuHdrDEL.TABLENAME]))
            {
                DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MOutLookAddinMenuHdrDEL.TABLENAME + "'").DefaultView;
                foreach (DataRowView drw in dvwCheck)
                {
                    if (!FieldCheck(dBAccess, MOutLookAddinMenuHdrDEL.Tables[MOutLookAddinMenuHdrDEL.TABLENAME].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.Update))
                    {
                        bReturn = false;
                        if (SysVar.ErrorMessage.Count == 0)
                        {
                            SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                        }
                        break;
                    }
                }
                bReturn &= CommonValid(dBAccess, MOutLookAddinMenuHdrDEL);

            }
            else
            {
                bReturn = false;
            }
            return bReturn;
        }
        #endregion
        #region Delete MOutLookAddinMenuHdr Valid
        public bool DeleteValid(DBAccess dBAccess, string strRecId, byte[] btOldRecStamp)
        {
            SysVar.ErrorMessage.Clear();
            if (!base.IsExistedAndNotChanged(dBAccess, "MOutLookAddinMenuHdr", strRecId, btOldRecStamp))
            {
                return false;
            }
            if (MsgBoxEx.Show(Msg.Select("Are you sure to delete this Printer? ", DataFunctions.GetMsg(this.GetType().Name, "MSG_DeleteThisPrinter")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return false;
            }
            return true;
        }
        #endregion
        #region Field Check
        public bool FieldCheck(DBAccess dbcon, DataRow dr, string strFieldName, ref string strErrPrompt, FormStatus formStatus)
        {
            strErrPrompt = string.Empty;
            if (strFieldName == null) return true;

            bool blnExistsField = false;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].Caption.ToUpper().Trim() == strFieldName.Trim().ToUpper())
                {
                    blnExistsField = true;
                    break;
                }
            }
            if (!blnExistsField) return true;
            string strFieldValue = CvtFunc.ToString(dr[strFieldName.Trim()]);

            switch (strFieldName.ToUpper().Trim())
            {
                #region UserName
                case "USERNAME":
                    if (!DataFunctions.CheckRecordExist(dbcon, "mUser", strFieldValue, "UserName", "UserName='" + strFieldValue + "'"))
                    {
                        strErrPrompt =Msg.Select("User not on file.", DataFunctions.GetMsg("UserNotExist"));
                        return false;
                    }
                    return true;
                #endregion
                #region MenuName
                case "OUTLOOKMENUNAME":
                    if (strFieldValue.TrimEnd() == "")
                    {
                        strErrPrompt = Msg.Select("Menu name cannot be empty.", DataFunctions.GetMsg(this.GetType().Name, "MSG_MenuNameCannotbeempty"));
                        return false;
                    }

                    if (!DataFunctions.CheckRecordExist(dbcon, "mOutLookAddinMenuHdr", strFieldValue, "OutlookMenuName", "UserName='" + SysVar.UserName   + "'"))
                    {
                        strErrPrompt =Msg.Select("Menu name already on file.", DataFunctions.GetMsg(this.GetType().Name, "MSG_MenuNameOnFile"));
                        return false;
                    }
                    return true;
                #endregion

                default:
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MOutLookAddinMenuHdrDEL.TABLENAME , strFieldName, ref strErrPrompt);
            }
        }
        #endregion
    }
}
